package com.bp.system.service.impl;

import com.bp.common.core.text.Convert;
import com.bp.system.domain.SysNotice;
import com.bp.system.mapper.SysNoticeMapper;
import com.bp.system.service.ISysDeptService;
import com.bp.system.service.ISysNoticeReadStatusService;
import com.bp.system.service.ISysNoticeService;
import com.bp.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公告 服务层实现
 * 
 * @author shaominjie
 * @date 2018-06-25
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {

    private final static Logger logger = LoggerFactory.getLogger(SysNoticeServiceImpl.class);
    @Resource
    private SysNoticeMapper noticeMapper;
    @Resource
    private ISysNoticeReadStatusService noticeReadStatusService;
    @Resource
    private ISysUserService userService;
    @Resource
    private ISysDeptService deptService;
    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        int flag =  noticeMapper.insertNotice(notice);
        if(flag >0) {
            insertReadRel(notice);
        }
        return flag;
    }



    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        SysNotice noticeOld = noticeMapper.selectNoticeById(notice.getNoticeId());
        if(noticeOld == null) {
            return 0;
        }
        try{
            noticeReadStatusService.deleteByNoticeId(noticeOld.getNoticeId(),notice.getUpdateBy());
            insertReadRel(notice);
        }catch (Exception e) {
            logger.error(e.getMessage());
        }

        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     * 
     * @param noticeIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        int flag =  noticeMapper.deleteNoticeByIds(noticeIds);
        try{
            noticeReadStatusService.deleteByNoticeIds(noticeIds);
        }catch (Exception e) {
            logger.error(e.getMessage());
        }
        return flag;
    }
    /**
     * 当前用户可看的公告
     * @param userId
     * @param notice
     * @return
     */
    @Override
    public List<SysNotice> selectNoticeByUser(Long userId, SysNotice notice){
      return null;
    }

    private void insertReadRel(SysNotice notice) {
    }
}
